﻿using System;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using HYBatchPrinter.Commands;

namespace HYBatchPrinter
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class Command : IExternalCommand
	{
		[DllImport("User32.dll")]
		private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

		[DllImport("User32.dll", EntryPoint = "SendMessageA")]
		private static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (!PrinterFunctionManager.Printting(commandData))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static void DisableEvent(object sender, ExecutedEventArgs args)
		{
		}

		public static Element findPrintSettingByName(string sName, Document rvtDoc)
		{
			Element result = null;
			if (!string.IsNullOrEmpty(sName))
			{
				foreach (ElementId elementId in rvtDoc.GetPrintSettingIds())
				{
					Element element = rvtDoc.GetElement(elementId);
					if (element.Name.Equals(sName))
					{
						result = element;
						break;
					}
				}
			}
			return result;
		}

		private static PaperSize GetRvtPaperSizeByName(PrintManager rvtPrintMgr, string sName)
		{
			PaperSize result = null;
			foreach (object obj in rvtPrintMgr.PaperSizes)
			{
				PaperSize paperSize = (PaperSize)obj;
				if (paperSize.Name.Equals(sName))
				{
					result = paperSize;
					break;
				}
			}
			return result;
		}

		public static bool createDefaultPrintSetting(string sName, UIApplication revUiApp)
		{
			return false;
		}

		public static void findWnd_eventsHandlerMethod(object obj, EventArgs args)
		{
		}

		public static void setInsesionToCurrent(PrintManager prntMgr)
		{
			prntMgr.PrintSetup.CurrentPrintSetting = prntMgr.PrintSetup.InSession;
		}

		public static AddInCommandBinding m_CommandBinding;
	}
}
